package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.async.completion.manager.CompletionExecuteManager;
import com.micro.modual.entity.MessageSend;
import com.micro.modual.entity.MessageText;
import com.micro.modual.extension.MessageTextExtension;
import com.micro.modual.mapper.MessageTextMapper;
import com.micro.modual.model.bo.messagetext.MessageTextBO;
import com.micro.modual.model.dto.messagetext.MessageTextListDTO;
import com.micro.modual.service.MessageTextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>消息内容表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("messageTextService")
public class MessageTextServiceImpl extends AbstractBasicsServiceImpl<MessageText,Long> implements MessageTextService {

    @Autowired
    private MessageTextMapper messageTextMapper;

    @Autowired
    private CompletionExecuteManager completionExecuteManager;

    @Override
    public RespJsonPageData<MessageTextListDTO> findMessageTextList(Map<String, Object> params) {
        PageQuery pageQuery = new PageQuery(params);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<MessageText> messageTextList =  messageTextMapper.findMessageTextList(params);
        List<MessageTextListDTO> messageTextListDTOList = JacksonUtil.getList(JacksonUtil.toJson(messageTextList),MessageTextListDTO.class);
        return new RespJsonPageData<MessageTextListDTO>(messageTextListDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>新增消息内容表详情</h2>
     * @param messageTextBO {@link MessageTextBO} 消息内容表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addMessageText(MessageTextBO messageTextBO) {
        MessageText messageText = MessageTextExtension.converAddMessageText(messageTextBO);
        messageTextMapper.insertUseGeneratedKeys(messageText);
        //如果是指定发送,则存储要发送的信息到消息发送记录表中,否则消息记录表通过查询插入
        if(messageTextBO.getSendStatus().equals((byte)1) && messageTextBO.getSendType().equals((byte) 2)){
            //异步发送
//            List<MessageSend> messageSendList = MessageExtension.convertAddMessageSend(messageTextBO,messageText);
//            messageSendMapper.insertListKeySelectiveMapper(messageSendList);
        }

        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改消息内容表详情</h2>
     * @param messageTextBO  {@link MessageTextBO} 消息内容表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editMessageText(MessageTextBO messageTextBO) {
        MessageText messageText = MessageTextExtension.converEditMessageText(messageTextBO);
        messageTextMapper.updateByPrimaryKeySelective(messageText);
        //如果是指定发送,则存储要发送的信息到消息发送记录表中,否则消息记录表通过查询插入
        if(messageTextBO.getSendStatus().equals((byte)1) && messageTextBO.getSendType().equals((byte) 2)){
//            //异步发送
//            List<MessageSend> messageSendList = MessageExtension.convertAddMessageSend(messageTextBO,messageText);
//            messageSendMapper.insertListKeySelectiveMapper(messageSendList);
        }
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除消息内容表信息</h2>
     * @param id  消息内容表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteMessageText(Long id) {
        messageTextMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

}
